package cn.jiahua.test15;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

    public static void main(String[] args) {
        //3个方法创建线程
     //  ExecutorService executorPool = Executors.newSingleThreadExecutor();// 单个线程的池子
     //  ExecutorService executorPool = Executors.newFixedThreadPool(3);
        ExecutorService executorPool = Executors.newCachedThreadPool();  //自动扩展


        try {
            for (int i = 0; i < 10000; i++) {
                //使用线程池，使用线程池创建线程
                executorPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "ok");
                });

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //线程池用完了，程序结束，关闭线程池
            executorPool.shutdown();
        }
    }
}
